import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author YuanYuan
 * @Date 2022/11/22
 * @Time 11:22
 */
@WebServlet("/message")
public class MessageWall extends HttpServlet {
    ObjectMapper objectMapper = new ObjectMapper();
    //处理发送给服务器的储存请求
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取req的内容,并存到message中
        Message message = objectMapper.readValue(req.getInputStream(),Message.class);
        try {
            save(message);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        System.out.println("提交数据成功: from=" + message.getFrom()
                + ", to=" + message.getTo() + ", message=" + message.getMessage());
    }

    //处理响应
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf8");
        try {
            resp.getWriter().write(objectMapper.writeValueAsString(load()));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //存储到数据库中
    private void save(Message message) throws SQLException {
        DataSource dataSource = DBUtil.getDataSource();
        //建立连接
        Connection connection = dataSource.getConnection();

        //构造sql
        String sql = "insert into message value(?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,message.getFrom());
        preparedStatement.setString(2,message.getTo());
        preparedStatement.setString(3,message.getMessage());
        //注入sql
        int ret = preparedStatement.executeUpdate();
        System.out.println(ret);

        //关闭连接
        preparedStatement.close();
        connection.close();
    }
    //从数据库读取
    private List<Message> load() throws SQLException {
        DataSource dataSource = DBUtil.getDataSource();
        //建立连接
        Connection connection = dataSource.getConnection();
        //构造sql
        String sql = "select * from message";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //注入sql
        ResultSet resultSet = preparedStatement.executeQuery();
        List<Message> list = new ArrayList<>();
        while(resultSet.next()) {
            Message message = new Message();
            message.setFrom(resultSet.getString("from"));
            message.setTo(resultSet.getString("to"));
            message.setMessage(resultSet.getString("message"));
            list.add(message);
        }

        preparedStatement.close();
        connection.close();
        return list;
    }
}
